<?php

/**
 *   OPENSHOP 专题文件夹管理
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: zip.php,v 1.0 2009/06/19 09:36:58 modified $
 *   @author      FillBag <fillbag@hotmail.com>
 */

define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php');

//上传根目录
$user_root_path = str_replace('\\', '/', SOURCE_PATH).'data/users/';
//相对路径
$rela_path = $_REQUEST['path'];

admin_priv('zip');

if (empty($_REQUEST['act']) )
{
	$_REQUEST['act'] = 'list';
}

/* 服务器是否支持zip扩展调用,用来使用相应的文件上传解压功能 */
$loaded_exts = get_loaded_extensions();
$zip_enabled = in_array("zip", $loaded_exts)?TRUE:FALSE;

if ($zip_enabled) {	
	if ($_REQUEST['act'] == 'list' || $_REQUEST['act'] == 'into'){	    
	    if($_REQUEST['act'] == 'into')
	    {
	        $category = $_REQUEST['cat'];
	    }
	    $rela_path = $category==''? $rela_path : $rela_path.$category.'/';
	    
	    $handle = opendir($user_root_path.$rela_path);
	    $file_index = 0;
	    $arr_flie = array();
	    while ($file = readdir($handle)) {
	        if($file!='.' && $file!='..' )
	        {
	            $arr_flie[$file_index]['name'] = $file;
	            $arr_flie[$file_index]['time'] = @date("Y-m-d H:i:s",filectime($user_root_path.$rela_path.$file)); 
	                
	            if(is_dir($user_root_path.$rela_path.$file)){
	                $arr_flie[$file_index]['type'] = 'category';
	            }else{
	                $arr_flie[$file_index]['type'] = 'file';
	            }
	            $file_index++;
	        }
	    }
	    closedir($handle); 
	    
	    $url = $filesync_host.'/data/users/'.$rela_path;
	    $smarty->assign('user_url',$url);
	    $smarty->assign('rela_path',$rela_path);
	    $smarty->assign('arr_flie',$arr_flie);
	    
	    $cates = getCategorys(split ('[/]', substr($rela_path,0,strlen($rela_path)-1)));
	    $smarty->assign('cates',$cates);	    
	    $smarty->display("zip.tpl"); 
	    
	}
	
	/*------------------------------------------------------ */
	//-- 创建目录
	/*------------------------------------------------------ */
	elseif ($_REQUEST['act'] == 'add'){
	
	    $categaryName = $_REQUEST['categaryName'];
	    
	    $filename =  $user_root_path.$rela_path.$categaryName;
	
	    /*添加链接*/
	    $link[0]['text'] = '继续添加文件夹';
	    $link[0]['href'] = './zip.php?act=list&path='.$rela_path;
	
	    $link[1]['text'] = '返回文件夹列表';
	    $link[1]['href'] = './zip.php';
	    
	    //判断文件夹是否存在
	    if (file_exists($filename)) {
	        
	        sys_msg('该文件已经存在！', 0, $link);
	        
	    } else {
	        if (@!mkdir($filename,0777))
	        {
	            sys_msg('文件夹添加失败！', 0, $link);
	        }else 
	        {
	            sys_msg('文件夹添加成功！', 0, $link);
	        }
	    }
	    /* 清除缓存 */
	    clear_cache_files();  
	}
	
	/*------------------------------------------------------ */
	//-- 删除文件
	/*------------------------------------------------------ */
	elseif ($_REQUEST['act'] == 'delete'){
	
	    $name = $_REQUEST['name'];
	    $filename = $user_root_path.$rela_path.$name;
	
	    $link[0]['text'] = '返回文件夹';
	    $link[0]['href'] = './zip.php?act=list&path='.$rela_path;
	    
	    if(is_dir($filename)){
	        @d_rmdir($filename);
	        sys_msg('文件夹已经删除！', 0, $link);
	    }else{
	        @unlink($filename); 
	        sys_msg('文件已经删除！', 0, $link);
	    }
	
	}
	
	/*------------------------------------------------------ */
	//-- 上传文件
	/*------------------------------------------------------ */
	elseif ($_REQUEST['act'] == 'upload'){
	    
	    $tempPath=$user_root_path.$rela_path;
	    $extractPath=$user_root_path.$rela_path;
	    
	    if($_FILES["filename"]["name"]){
	        $file1 = $_FILES["filename"]["name"];
	        $file2 = $tempPath.$file1;
	        $flag=1;
	    }
	    $suffix = substr($file1,-4,4);
	
	    if($flag) $result = move_uploaded_file( $_FILES["filename"]["tmp_name"], $file2);
	
	    if( $result && ($suffix=='.zip')) {
	        set_time_limit(0);
	        $zip_filename = $file2;
	        
	        $zip_filename = key_exists('zip', $_GET) && $_GET['zip']?$_GET['zip']:$zip_filename;
	        $zip_filepath = $zip_filename;
	
	        if(!is_file($zip_filepath)){
	            die('file"'.$zip_filepath.'"not existed!');
	        }
	        
	        $zip = new ZipArchive();
	        $rs = $zip->open($zip_filepath);
	        
	        $zip->extractTo($extractPath);
	        $zip->close();
	        
	        $link[0]['text'] = '返回文件夹';
	        $link[0]['href'] = './zip.php?act=list&path='.$rela_path;
	        sys_msg('上传并解压成功！', 0, $link);
	        
	    }else {
	        $link[0]['text'] = '返回文件夹';
	        $link[0]['href'] = './zip.php?act=list&path='.$rela_path;
	        sys_msg('文件上传成功！', 0, $link);
	    }
	    
	}
	
	/*------------------------------------------------------ */
	//-- 修改文件名称
	/*------------------------------------------------------ */
	elseif ($_REQUEST['act'] == 'edit_file_name')
	{
	
	    $old_name   = $_POST['id'];
	    
	    $para = explode('**', $old_name);
	    $old_name = $para[0];
	    $rela_path = $para[1];
	    
	    $new_name = json_str_iconv(trim($_POST['val']));
	    
	    $old_name2 = $user_root_path.$rela_path.$old_name;
	    $new_name2 = $user_root_path.$rela_path.$new_name;
	    rename($old_name2,$new_name2);
	    
	    clear_cache_files();
	    make_json_result(stripslashes($new_name));
	
	}	
}else{
	$smarty->assign('zip_enable','not_support'); 
    $smarty->display("zip.tpl"); 	
}

function getCategorys($arr)
{
	$arr_temp = array();
	foreach ($arr as $key => $value)
	{
		$arr_temp[$key]['name'] = $value;
		$arr_temp[$key]['link'] = './zip.php?act=into&path='.getCategoryLink($arr,$value).'&cat='.$value;
	}
	return $arr_temp;
}

function getCategoryLink($arr,$name)
{
	$link = '';
	foreach( $arr as $value)
	{
		if($value==$name)
			break;
		else
			$link = $link.$value.'/';
	}
	return $link;
}

function d_rmdir($dirname) {
	if(!is_dir($dirname)) {
		return false;
	}
	$handle = @opendir($dirname);
	while(($file = @readdir($handle)) !== false){
		if($file != '.' && $file != '..'){
			$dir = $dirname . '/' . $file;
			is_dir($dir) ? d_rmdir($dir) : @unlink($dir);
		}
	}
	closedir($handle);
	return rmdir($dirname) ;
}
?>